import { Component, OnInit } from '@angular/core';
import { ToastController, Events } from '@ionic/angular';
import { ApiService } from '../api/api.service';
import { ApiUrl } from '../api/ApiUrl';
import { AnswerVO } from '../vo/Answervo';
import { Router } from '@angular/router';
import { Storage } from '@ionic/storage';
import { UserVO } from '../vo/UserVO';
import { CompanyVO } from '../vo/CompanyVO';


@Component({
  selector: 'app-login',
  templateUrl: './login.page.html',
  styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
  item: AnswerVO;
  userItem: Object;
  isShowLogin = true;
  isShowPwd = false;
  isShowUnit = false;
  loginname: any;
  password: any;
  checked = true;
  iusertype = '';
  r_password_qr = '';
  r_password = '';
  r_code = '';
  r_phone = '';
  r_loginname = '';
  r_ejunits = '';
  orgitems: object;
  iorgname = '0';
  corgname: string;
  verifyCode: any = {
    verifyCodeTips: '获取验证码',
    countdown: 60,
    disable: true
};
  constructor(private toastController: ToastController, public api: ApiService, private router: Router, private storage: Storage,
    private events: Events) { }

  ngOnInit() {
    this.getStorage();
  }
  // 倒计时
settime() {
  if (this.verifyCode.countdown === 1) {
  this.verifyCode.countdown = 60;
  this.verifyCode.verifyCodeTips = '获取验证码';
  this.verifyCode.disable = true;
  return;
  } else {
  this.verifyCode.countdown--;
  }

  this.verifyCode.verifyCodeTips = '重新获取(' + this.verifyCode.countdown + ')';
  setTimeout(() => {
  this.verifyCode.verifyCodeTips = '重新获取(' + this.verifyCode.countdown + ')';
  this.settime();
  }, 1000);
}
getCode() {
  if (this.r_phone === '') {
    this.presentToast('请输入正确的手机号');
  return;
  }
  const testpattern1 = /^[1][345789][012356789][0-9]+$/.test(this.r_phone);
  if (!testpattern1) {
      this.presentToast('请输入正确的手机号');
      return;
  }
  // 发送验证码成功后开始倒计时
  this.api.getItem<AnswerVO>(ApiUrl.getCode, {'cphone': this.r_phone}).subscribe(response => {
    this.item = response;
    if (this.item.success) {
      this.presentToast(this.item.msg);
    } else {
      this.presentToast(this.item.msg);
    }
  }, () => {
    console.log('出错了');
  });
  this.verifyCode.disable = false;
  this.settime();

}

  ionSegmentChange() {
    this.isShowLogin = !this.isShowLogin;
  }
  checklogin() {
    if (this.loginname.length === 0) {
      this.presentToast('请输入用户名');
      return;
    }
    if (this.password.length === 0) {
      this.presentToast('请输入密码');
      return;
    }
    this.toLogin(this.loginname, this.password);
  }
  checkRegist() {
    console.log(this.iorgname);
    if (this.iusertype !== '2' && this.iusertype !== '3') {
      this.presentToast('请选择用户类型');
      return;
    }
    if (this.iorgname === '0' ) {
      this.presentToast('请选择用户所属机构');
      return;
    }
    if (this.iusertype === '2') {
      if (this.r_ejunits.length === 0) {
        this.presentToast('请输入二级单位');
        return;
      }
    }
    if (this.r_loginname.length === 0) {
      this.presentToast('请输入用户名');
      return;
    }
    if (this.r_code.length === 0) {
      this.presentToast('请输入验证码');
      return;
    }
    if (this.r_password.length === 0) {
      this.presentToast('请输入密码');
      return;
    }
    if (this.r_password_qr.length === 0) {
      this.presentToast('请输入确认密码');
      return;
    }
    if (this.r_password_qr !== this.r_password) {
      this.presentToast('两次密码输入不一致');
      return;
    }
    this.toRegist();
  }
  toLogin(loginname, password) {
    this.api.getItem<AnswerVO>(ApiUrl.toLogin, { 'loginname': loginname, 'password': password }).subscribe(response => {
      this.item = response;
      if (this.item.success) {
        this.presentToast(this.item.msg);
        if (this.checked) {
          this.storage.set('loginname', loginname);
          this.storage.set('password', password);
        } else {
          this.storage.remove('loginname');
          this.storage.remove('password');
        }
        ApiUrl.user = response.user;
        this.changeMessageTabBadge();
        this.router.navigate(['/tabs']);
      } else {
        this.presentToast(this.item.msg);
      }
    }, () => {
      console.log('出错了');
    });
  }
  // 发布事件
  changeMessageTabBadge() {
    this.events.publish('userlogin:change', true, Date.now());
  }
  toRegist() {
    this.api.getItem<AnswerVO>(ApiUrl.toRegist, {
      'iusertype': this.iusertype,
      'loginname': this.r_loginname,
      'password': this.r_password,
      'cphone': this.r_phone, 'code': this.r_code, 'cunit': this.r_ejunits, 'iorgname': this.iorgname
    }).subscribe(response => {

      this.item = response;
      if (this.item.success) {
        this.presentToast(this.item.msg);
        this.router.navigate(['/tabs']);
      } else {
        this.presentToast(this.item.msg);
      }
      // this.router.navigate(['/tabs']);
    }, () => {
      console.log('出错了');
    });
  }
  getStorage() {
    this.storage.get('loginname').then((val) => {
      this.loginname = val;
    });
    this.storage.get('password').then((val) => {
      this.password = val;
    });
  }

  async presentToast(msg) {
    const toast = await this.toastController.create({
      message: msg,
      duration: 2000
    });
    toast.present();
  }
  packagTypeChange() {
    if (this.iusertype === '2') {
      this.getOrgItems(2);
      this.isShowUnit = true;
    }
    if (this.iusertype === '3') {
      this.getOrgItems(3);
      this.isShowUnit = false;
    }
  }
  iorgnameChange() {
    console.log(this.iorgname);
  }
  // 动态获取单位列表
  getOrgItems(itype) {
    this.api.getItem<AnswerVO>(ApiUrl.getOrgItems, { 'itype': itype }).subscribe(response => {
      this.orgitems = response.data;
    }, () => {
      console.log('出错了');
    });
  }
}
